<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="销售单号" prop="somDjbh">
        <el-input
          v-model="queryParams.molSodh"
          placeholder="请输入销售单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="父项代码" label-width="80px" prop="molFxdm">
        <el-input
          v-model="queryParams.molFxdm"
          clearable
          size="small"
          placeholder="请输入父项代码"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="子项代码" label-width="80px" prop="molZxdm">
        <el-input
          v-model="queryParams.molZxdm"
          clearable
          size="small"
          placeholder="请输入子项代码"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label-width="125px" prop="totalOrder">
        <span slot="label">
          <el-tooltip
            content="综合查询包含父项子项物料代码查询,品名规格"
            placement="top"
          >
            <i class="el-icon-question"></i>
          </el-tooltip>
          综合查询
        </span>
        <el-input
          v-model="queryParams.totalOrder"
          placeholder="请输入物料代码或品名规格"
          clearable
          style="width: 400px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="single"
          @click="handleCheck"
          v-hasPermi="['produce:materialAdjust:add']"
        >
          修改采购属性以及数量
        </el-button>
      </el-col>
      <el-col :span="2.2" style="position: absolute; right: 0px; top: 0">
        <Page
          style="margin-right: 0px"
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total"
          @on-change="pageChange"
          simple
        ></Page>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      highlight-current-row
      v-loading="loading"
      :data="somList"
      border
      stripe
      ref="materialAdjustTable"
      @row-click="clickRow"
      :height="tableH"
      @selection-change="handleSelectionChange"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="销售单号"
        :show-overflow-tooltip="true"
        min-width="10%"
        align="center"
        prop="molSodh"
      />
      <el-table-column
        label="父项物料代码"
        :show-overflow-tooltip="true"
        min-width="10%"
        align="center"
        prop="molFxdm"
      />
      <el-table-column
        label="父项物料品名规格"
        :show-overflow-tooltip="true"
        min-width="20%"
        prop="molFxdmPmgg"
      />
      <el-table-column
        label="子项物料代码"
        :show-overflow-tooltip="true"
        min-width="10%"
        align="center"
        prop="molWldm"
      />
      <el-table-column
        label="子项物料品名规格"
        :show-overflow-tooltip="true"
        min-width="20%"
        prop="molPmgg"
      />
      <el-table-column
        label="订单数量"
        :show-overflow-tooltip="true"
        min-width="10%"
        align="center"
        prop="molDdsl"
      />
      <el-table-column
        label="需求数量"
        :show-overflow-tooltip="true"
        min-width="10%"
        align="center"
        prop="molScsl"
      />
      <el-table-column
        label="子项物料采购属性"
        :show-overflow-tooltip="true"
        min-width="10%"
        align="center"
        prop="molCgsx"
      >
        <template #default="scope">
          <dict-tag :options="bomCgsxOptions" :value="scope.row.molCgsx" />
        </template>
      </el-table-column>
    </el-table>

    <!-- 材料明细属性调整 -->
    <el-dialog
      :title="title"
      v-model="open"
      :close-on-click-modal="false"
      draggable
      width="780px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-col :span="12">
          <el-form-item label="销售单号" prop="molSodh">
            <el-input v-model="form.molSodh" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="采购属性" prop="molTzCgsx">
            <el-select v-model="form.molTzCgsx" placeholder="请选择采购属性">
              <el-option
                v-for="dict in bomCgsxOptionsNromal"
                :key="dict.cgId"
                :label="dict.cgName"
                :value="dict.cgId"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="父项物料代码" prop="molFxdm">
            <el-input v-model="form.molFxdm" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="父项物料品名规格" prop="molFxdmPmgg">
            <el-input v-model="form.molFxdmPmgg" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="子项物料代码" prop="molWldm">
            <el-input v-model="form.molWldm" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="子项物料品名规格" prop="molPmgg">
            <el-input v-model="form.molPmgg" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="调整数量" prop="molScsl">
            <el-input
              type="number"
              v-model="form.molScsl"
              @input="changValue(form.molScsl)"
            />
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
          <el-form-item v-if="form.molTzCgsx===5" label="厂商代码" prop="molCsm" >
            <el-select v-model="form.molCsm"  filterable>
              <el-option v-for="item in csOptions" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue"></el-option>
            </el-select>
          </el-form-item>
        </el-col> -->
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getAllCgList, getAllCgListNormal } from "@/api/project/mstr";
import {
  edit,
  getBomDownIsMM,
  getMaterielAdjustMol,
} from "@/api/produce/materialAdjust";
import { getCs } from "@/api/putStorage/putStorageAdd";

export default {
  name: "MaterialAdjust",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      tableH: document.body.clientHeight - 230,
      //采购属性
      bomCgsxOptions: [],
      csOptions: [],

      // 日期范围
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 营销订单维护表格数据
      somList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      bomCgsxOptionsNromal: [],

      // 查询参数
      queryParams: {
        totalOrder: null,
        pageNum: 1,
        pageSize: 10,
        molSodh: null,
        molFxdm: null,
        molZxdm: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        molScsl: [
          { required: true, message: "调整数量不能为空", trigger: "blur" },
        ],
        molTzCgsx: [
          { required: true, message: "采购属性不能为空", trigger: "blur" },
        ],
        molCsm: [
          { required: true, message: "请选择厂商代码", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    getCs().then((response) => {
      this.csOptions = response.data;
      for (let item of this.csOptions) {
        item.dictLabel = item.csmCs;
        item.listClass = "default";
        item.dictValue = item.csmCsdm;
      }
    });
    //得到采购属性
    getAllCgList().then((response) => {
      this.bomCgsxOptions = response.data;
      for (let i of this.bomCgsxOptions) {
        i.dictLabel = i.cgName;
        i.dictValue = i.cgId + "";
        i.listClass = "default";
      }
    });

    getAllCgListNormal().then((response) => {
      for (let item of response.data) {
        if (item.cgId === 4 || item.cgId === 5 || item.cgId === 7) {
          this.bomCgsxOptionsNromal.push(item);
        }
      }
    });
  },
  methods: {
    /** 查询营销订单维护列表 */
    getList(row) {
      this.loading = true;
      getMaterielAdjustMol(this.queryParams).then((response) => {
        this.somList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    pageChange(page) {
      this.queryParams.pageNum = page;
      this.getList();
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        molId: null,
        molSodh: null,
        molFxdm: null,
        molWldm: null,
        molFxdmPmgg: null,
        molPmgg: null,
        molScsl: null,
        molCsm: null,
        molXqsl: null,
        molQrsl: null,
        molTzCgsx: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.dateRange = null;
      this.queryParams.somJhrqBegin = null;
      this.queryParams.somJhrqEnd = null;
      this.handleQuery();
    },
    clickRow(row) {
      this.$refs.materialAdjustTable.toggleRowSelection(row);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.currentData = selection;
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },

    //审核
    handleCheck() {
      this.form = {
        molId: this.currentData[0].molId,
        molSodh: this.currentData[0].molSodh,
        molFxdm: this.currentData[0].molFxdm,
        molWldm: this.currentData[0].molWldm,
        molFxdmPmgg: this.currentData[0].molFxdmPmgg,
        molPmgg: this.currentData[0].molPmgg,
        molScsl: this.currentData[0].molScsl,
        molXqsl: this.currentData[0].molXqsl,
        molQrsl: this.currentData[0].molQrsl,
      };

      this.title = "采购属性调整";

      this.open = true;
    },
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          getBomDownIsMM(this.form).then((res) => {
            if (res.data.length > 0 && this.form.molTzCgsx == 5) {
              this.$confirm("是否外发当前成型的原辅料？", "请确认", {
                distinguishCancelAndClose: true,
                confirmButtonText: "外发",
                cancelButtonText: "不外发",
              })
                .then(() => {
                  this.form.downUnfold = 1;
                  edit(this.form).then((response) => {
                    this.open = false;
                    this.msgSuccess("修改成功");
                    this.getList();
                  });
                })
                .catch((action) => {
                  if (action === "cancel") {
                    this.form.downUnfold = 0;
                    edit(this.form).then((response) => {
                      this.open = false;
                      this.msgSuccess("修改成功");
                      this.getList();
                    });
                  }
                });
            } else {
              this.form.downUnfold = 0;
              edit(this.form).then((response) => {
                this.open = false;
                this.msgSuccess("修改成功");
                this.getList();
              });
            }
          });
        }
      });
    },

    changValue(row) {
      if (row > this.form.molXqsl - this.form.molQrsl) {
        this.form.molScsl = this.form.molXqsl - this.form.molQrsl;
      } else if (row < 1) {
        this.form.molScsl = 1;
      }
    },
  },
};
</script>

<style lang="scss" scoped></style>
